package com.guchenbo.example.io;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @author guchenbo
 * @date 2022/4/28
 */
public class FileIOTest {

    public static void main(String[] args) throws Exception {
        List<String> books = BookUtils.getBooks();
        String dataPath = "/tmp";
        GuFileSystem gfs = new GuFileSystem();

//        testWrite(dataPath, books, gfs);
        testRead(dataPath, books, gfs);

        String s = "Vim中文文档";
        byte[] bs = s.getBytes(StandardCharsets.US_ASCII);
        System.out.println(new String(bs, StandardCharsets.US_ASCII));

    }

    private static void testWrite(String dataPath, List<String> books, GuFileSystem gfs) throws IOException {
        for (int i = 0; i < 3000; i++) {
            gfs.writeData(dataPath, books);
        }

    }

    private static void testRead(String dataPath, List<String> books, GuFileSystem gfs) throws Exception {
        int size = books.size();
        for (int i = 0; i < 82582; i++) {
            int index = i + 1;
            System.out.println(index);
            String data = gfs.readData(dataPath, index);
            int idx = index % size - 1;
            if (idx == -1) {
                idx += size;
            }
            String b = books.get(idx);
            System.out.println(data);
            System.out.println(b);

            if (!b.equals(data)) {
                break;
            }
        }
    }

}
